package com.momo.elasticsearch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.momo.elasticsearch.entity.EcDataLogDO;
import com.momo.elasticsearch.entity.EcMapper;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by MOMO on 2019/3/14.
 */
@Service
@Slf4j
public class HZService {
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private EcMapper ecMapper;

    public void count(String index, String type) {
        List<Long> longid = Lists.newArrayList();
        longid.add(302882624941199360L);
        longid.add(302882625016696832L);
        try {
            List<String> list = ecMapper.getDeviceNos();
            //索引别名
            SearchRequest searchRequest = new SearchRequest("sys_core_alias");
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

            //我们将size=0设置为不显示搜索结果，因为我们只想看到响应中的聚合结果
            searchSourceBuilder.size(0);
            boolQueryBuilder.must(QueryBuilders.termsQuery("longid", longid));
            searchSourceBuilder.query(boolQueryBuilder);
            ValueCountAggregationBuilder sumAggregationBuilder = AggregationBuilders.count("countcreateTime").field("longid");
            searchRequest.source(searchSourceBuilder);
            searchSourceBuilder.aggregation(sumAggregationBuilder);
            searchRequest.types(type);
            log.info(searchRequest.toString());
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            log.info("耗时：{}", searchResponse.getTook());
            Map<String, Aggregation> aggMap = searchResponse.getAggregations().asMap();
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            log.info("searchHits.length {}",searchHits.length);
            for (SearchHit hit : searchHits) {
                log.info("hit.toString()"+hit.toString());

            }
            aggMap.forEach((k, v) -> {
                ParsedValueCount teamAgg = (ParsedValueCount) aggMap.get("countcreateTime");
                log.info("数量为{}", teamAgg.getValue());
                log.info(JSONObject.toJSONString(teamAgg));
            });
        } catch (IOException e) {
            log.error("查询数量 异常:{}", e.getMessage());
        }

    }

}
